package com.infovane.action;

import java.util.ArrayList;
import java.util.List;


import com.infovane.base.BaseAction;
import com.infovane.model.PageModel;
import com.infovane.model.TArticle;
import com.infovane.model.TArticleFile;
import com.infovane.service.ArticleFileService;
import com.infovane.service.ArticleService;
import com.infovane.utils.PagingModule;
import com.infovane.utils.SystemConstant;

public class ArticleAction extends BaseAction{
	
	private ArticleService articleService;
	
	
	public ArticleService getArticleService() {
		return articleService;
	}


	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	
	private ArticleFileService articleFileService;
	
	public ArticleFileService getArticleFileService() {
		return articleFileService;
	}


	public void setArticleFileService(ArticleFileService articleFileService) {
		this.articleFileService = articleFileService;
	}


	/**
	 * 分页获取文章数据
	 * */
	public String getArticleList(){
		String strPageNo=httpServletRequest.getParameter("currentPage");
		int currentPage;
		if(strPageNo==null||strPageNo.equals("")){
			currentPage=1;
		}else{
			try{
			currentPage=Integer.parseInt(strPageNo.intern());
			}catch(NumberFormatException e){
				currentPage=1;
			}
		}
		List<TArticle> list=this.articleService.getArticleList(currentPage,SystemConstant.PAGE_SIZE);
		int count=this.articleService.getCount();
		int maxPage=PagingModule.maxPageSize(count);
		PageModel<TArticle> pm=new PageModel<TArticle>(list,maxPage);
		httpServletRequest.setAttribute("pm", pm);
		return SUCCESS;
	}
	
	/**
	 * 根据文章ID加载文章对象
	 * */
	public String getArticle(){
		Long id;
		try{
			id=Long.parseLong(httpServletRequest.getParameter("articleId"));
		}catch(NullPointerException e){
			String warn="对不起!没有该文章。";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}catch(NumberFormatException e){
			String warn="对不起!没有该文章。";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}catch(Exception e){
			String warn="对不起!没有该文章。";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}
		if(id<0){
			String warn="对不起!没有该文章。";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}else{
			TArticle article=new TArticle();
			article=(TArticle) this.articleService.get(TArticle.class, id);
			if(article!=null){
				httpServletRequest.setAttribute("ARTICLE", article);
				String aFileIndex=article.getArticleFileIndex();
				if(aFileIndex!=null&&!aFileIndex.equals("not")){
					List<TArticleFile> afileList=new ArrayList<TArticleFile>();
					afileList=this.articleFileService.get(aFileIndex);
					httpServletRequest.setAttribute("AFILE", afileList);
				}
				Long click=article.getArticleClick();
				if(click==null){
					click=0L;
				}
				article.setArticleClick(click+1);
				this.articleService.update(article);
				return SUCCESS;
			}
		}
		return ERROR;
	}
	
	/**
	 * 根据关键字查询文章
	 * */
	public String getQuery(){
		String keywords;
		try{
			keywords=(String)httpServletRequest.getParameter("keywords");
		}catch(NullPointerException e){
			String warn="请输入关键字!";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}catch(Exception e){
			String warn="请输入关键字!";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}
		if(keywords!=null&&keywords.equals("")){
			String warn="请输入关键字!";
			httpServletRequest.setAttribute("WARN", warn);
			return ERROR;
		}else{
			List<TArticle> list=this.articleService.getQuery(keywords);
			if(list!=null&&list.size()>0){
				httpServletRequest.setAttribute("QRESULT",list);
				return SUCCESS;
			}else{
				return INPUT;
			}
		}
		
	}
}
